#include "GLArrayBuffer.h"

#include <Rz/Graphics/Render/Libraries/OpenGL/GL.h>
#include <Rz/Graphics/Render/Libraries/OpenGL/GLRenderDevice.h>


namespace Rz { namespace Graphics { namespace Render { namespace Libraries { namespace OpenGL {

GLArrayBuffer::GLArrayBuffer(IRenderDevice* renderDevice, BufferObjectType type)
	: _usageHint(GL_STATIC_DRAW)
{
	this->SetRenderDevice(renderDevice);
	this->SetBufferType(type);

	_nativeBufferType = TranslateBufferType(type);
	_bufferHandle = GL::GenBuffer();
}

GLArrayBuffer::~GLArrayBuffer()
{
	GL::DeleteBuffer(_bufferHandle);
	GL::BindBuffer(_nativeBufferType, 0);
}

void GLArrayBuffer::GetData(void* data, u32 count, u32 startIndex) const
{
	// TODO: NotImplemented
	RZ_UNUSED(data);
	RZ_UNUSED(count);
	RZ_UNUSED(startIndex);
}

void GLArrayBuffer::SetData(const void* data, u32 dataLength, u32 startIndex, u32 offsetInBytes)
{
	// TODO: SetData properly
	RZ_UNUSED(startIndex);
	RZ_UNUSED(offsetInBytes);

	this->SetSize(dataLength);

	GL::BindBuffer(_nativeBufferType, _bufferHandle);
	GL::BufferData(_nativeBufferType, dataLength, data, _usageHint);
}

void GLArrayBuffer::Bind()
{
	GL::BindBuffer(_nativeBufferType, _bufferHandle);
}

void GLArrayBuffer::Unbind()
{
	GL::BindBuffer(_nativeBufferType, 0);
}

GLenum GLArrayBuffer::TranslateBufferType(BufferObjectType bufferObjectType)
{
	switch (bufferObjectType)
	{
		default:
		case BufferObjectType::None:
		case BufferObjectType::IndexBuffer:		return GL_ELEMENT_ARRAY_BUFFER;
		case BufferObjectType::UniformBuffer:	return GL_UNIFORM_BUFFER;
		case BufferObjectType::VertexBuffer:	return GL_ARRAY_BUFFER;
	}
}


} } } } }

